package com.tarasenko.model.entities;

import javax.faces.context.FacesContext;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.tarasenko.web.controller.SprintManager;

public class SprintNameUniquenessConstraintValidator implements ConstraintValidator<SprintNameUniquenessConstraint, String> {

    public boolean isValid(String value, ConstraintValidatorContext ctx) {
        
        FacesContext context = FacesContext.getCurrentInstance();
        SprintManager sprintManager = (SprintManager)
                context.getApplication().evaluateExpressionGet(context, 
                "#{sprintManager}", SprintManager.class);
        boolean result = true;
        String message = sprintManager.
                checkUniqueSprintNameApplicationValidatorMethod(value);
        if (null != message) {
            result = false;
	    ConstraintValidatorContext.ConstraintViolationBuilder builder =
		ctx.buildConstraintViolationWithTemplate(message);
	    builder.addConstraintViolation();
        }

        
        return result;
    }

    public void initialize(SprintNameUniquenessConstraint arg0) {
    }
    
}
